<template>
  <div class="box">
    <div class="card-container">
      <el-row :gutter="10">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-col :span="19" :offset="0">
          <el-form-item label="项目简称" prop="shortName">
            <el-input v-model="queryParams.shortName" placeholder="请输入项目简称" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <!-- <el-form-item label="项目类型" prop="type">
              <el-select v-model="queryParams.type" placeholder="请选择项目类型" clearable size="small">
                <el-option v-for="dict in dict.type.project_type" :key="dict.value"
                  :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item> -->
          <el-form-item label="是否启用" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
              <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5" :offset="0">
          <el-form-item>
            <el-button  icon="el-icon-search" size="mini" @click="handleQuery">搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    </div>
   


    <div class="card-container">
      <div class="body-title">
        <div class="body-title-left">
          项目管理
        </div>
        <div class="body-title-right">
          <el-button   icon="el-icon-plus" size="mini" v-hasPermi="['project:add']"
          @click="handleAdd">新增</el-button>
          <el-button   icon="el-icon-edit" size="mini" :disabled="single" v-hasPermi="['project:edit']"
          v-show="!multiple" @click="handleUpdate">修改</el-button>
          <el-button   icon="el-icon-delete" size="mini" :disabled="multiple"
          v-hasPermi="['project:remove']" v-show="!multiple" @click="handleDelete">删除</el-button>
        </div>
      </div>

      <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" :selectable="checkSelectable" width="55" align="center" />
        <el-table-column label="序号" align="center" prop="projectId" />
        <el-table-column label="项目编号" align="center" prop="code" />
        <el-table-column label="项目简称" align="center" prop="shortName" />
        <el-table-column label="项目全称" align="center" prop="name" />
        <!-- <el-table-column label="项目类型" align="center" prop="type">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.project_type" :value="scope.row.type" />
          </template>
        </el-table-column> -->
        <el-table-column label="计划设备数" align="center" prop="number" />
        <el-table-column label="已关联设备数" align="center" prop="setNumber" />
        <el-table-column label="是否启用" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" v-hasPermi="['project:edit']"
              @click="handleUpdate(scope.row)">修改</el-button>
            <!-- <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['project:remove']">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    <!-- 添加或修改项目信息对话框 -->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="500px" append-to-body>
      <div class="card-container">
        <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="项目编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入项目编号" />
        </el-form-item>
        <el-form-item label="项目简称" prop="shortName">
          <el-input v-model="form.shortName" placeholder="请输入项目简称" />
        </el-form-item>
        <el-form-item label="项目全称" prop="name">
          <el-input v-model="form.name" placeholder="请输入项目名称" />
        </el-form-item>
        <!-- <el-form-item label="项目类型" prop="type">
          <el-select v-model="form.type" style="width: 370px" placeholder="请选择项目类型">
            <el-option v-for="dict in dict.type.project_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="计划设备数" prop="number">
          <!-- <el-input v-model="form.number" type="number" placeholder="请输入计划设备数" /> -->
          <el-input v-model="form.number" oninput="value=value.replace(/[^0-9]/g,'')" maxlength="6"
            placeholder="请输入计划设备数" />
        </el-form-item>
        <el-form-item label="是否启用" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      </div>
      
      <div slot="footer" class="dialog-footer">
        <el-button  @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProject,
  getProject,
  delProject,
  addProject,
  updateProject,
} from '@/api/project/project';
const numPass = (rule, value, callback) => {
  if (!/^[0-9a-zA-Z]+$/.test(value)) {
    callback(new Error('请输入正确的格式'));
  } else {
    callback();
  }
};
export default {
  name: 'Project',
  dicts: ['sys_normal_disable', 'project_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目信息表格数据
      projectList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        shortName: null,
        type: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: '项目编号不能为空', trigger: 'blur' },
          { validator: numPass, trigger: 'blur' },
        ],
        shortName: [
          { required: true, message: '项目简称不能为空', trigger: 'blur' },
        ],
        name: [
          { required: true, message: '项目全称不能为空', trigger: 'blur' },
        ],
        type: [
          { required: true, message: '请选择项目类型', trigger: 'change' },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkSelectable(row) {
      return row.setNumber <= 0
    },
    /** 查询项目信息列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then((response) => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        code: null,
        shortName: null,
        name: null,
        type: null,
        number: null,
        status: '0',
        delFlag: null,
        delTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.projectId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加项目信息';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const projectId = row.projectId || this.ids;
      getProject(projectId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = '修改项目信息';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.projectId != null) {
            updateProject(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectIds = row.projectId || this.ids;
      this.$modal
        .confirm('是否确认删除当前项目？')
        .then(function () {
          return delProject(projectIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'zzy/project/export',
        {
          ...this.queryParams,
        },
        `project_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  height: 100%;
  padding: 20px;
}

.body-title {
  display: flex;
  position: relative;
  // justify-content: space-between;
  align-items: center;
}

.body-title-left {
  height: 100%;
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 600;
  color: #fff;
  line-height: 64px;
}

.body-title-right {
  position: absolute;
  right: 0;
}
</style>
